package com.googlecode.mcwalle.response;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * 用于封装stats slabs的返回结果
 * 
 * @author Li Weibo (weibo.leo@gmail.com) //I believe spring-brother
 * @since 2011-10-28 下午05:53:52
 */
public class StatsSlabsResponse {

	private int activeSlabs;
	
	private long totalMalloced;
	
	private Map<Integer, SlabInfo> slabInfos = new HashMap<Integer, SlabInfo>();
	
	public StatsSlabsResponse(TextLineResponse res) {
		for (String line : res) {
			String[] ss = line.split(":");
			if (ss.length == 2) {
				int slabNumber = Integer.parseInt(ss[0].split(" ")[1]);
				SlabInfo info = slabInfos.get(Integer.valueOf(slabNumber));
				if (info  == null) {
					info = new SlabInfo();
					info.setSlabNumber(slabNumber);
					slabInfos.put(Integer.valueOf(slabNumber), info);
				}
				//TODO parse detail infos
			} else if (ss.length == 1) {
				if (line.startsWith("STAT active_slabs")) {
					this.activeSlabs = Integer.parseInt(line.split(" ")[2]);
				} else if (line.startsWith("STAT total_malloced")) {
					this.totalMalloced = Long.parseLong(line.split(" ")[2]);
				}
			} else {
				throw new RuntimeException("Unexcepted line: " + line);
			}
		}
	}
	
	public Iterator<Integer> slabNumberIterator() {
		return slabInfos.keySet().iterator();
	}

	public int getActiveSlabs() {
		return activeSlabs;
	}

	public void setActiveSlabs(int activeSlabs) {
		this.activeSlabs = activeSlabs;
	}

	public long getTotalMalloced() {
		return totalMalloced;
	}

	public void setTotalMalloced(long totalMalloced) {
		this.totalMalloced = totalMalloced;
	}
	
}
